<script setup>
import { ref, reactive, watch } from 'vue'
import config from '@/assets/js/config'
import { showMsg, showConfirm } from '@/assets/js/utils'
import { getDeviceList, deleteDevice } from '@/assets/api/device'
import { useRoute } from 'vue-router'
const route = useRoute()
const deviceClassify = 2
const deviceClassId = ref(route.params.deviceClass)
watch(route, () => {
    deviceClassId.value = route.params.deviceClass
    if (
        deviceClassId.value &&
        route.path == `/fireControl/device/${deviceClassId.value}`
    ) {
        filterForm.deviceClass = deviceClassId.value
        getData()
    }
})
const deviceNumber = ref()
const filterForm = reactive({
    deviceClassify,
    deviceClass: deviceClassId.value,
})

const loading = ref(false)
const currentPage = ref(1)
const pageSize = 10
const total = ref(0)
const dataList = ref([])
function getData(page = 1) {
    loading.value = true
    const params = Object.assign(
        {
            page,
            limit: pageSize,
            is_children: 1,
            trans_device_code: route.query.number,
        },
        filterForm
    )
    getDeviceList(params).then((res) => {
        loading.value = false
        const { Items, totalSize } = res.data
        dataList.value = Items
        currentPage.value = res.data.currentPage
        total.value = totalSize
    })
}
getData(1)
function handleCurrentChange(e) {
    getData(e)
}
function searchAction() {
    getData(1)
}

// 导入
const importModalVisible = ref(false)
import importModal from '@/components/fireControl/deviceImport.vue'
function onImportSuccess() {
    getData(currentPage.value)
}
//导出
// function exportAction() {
//     let name = filterForm.name || ''
//     let number = filterForm.number || ''
//     let url = `${config.baseApi}/uploadFile/getDeviceTemplte?&deviceClassify=${deviceClassify}&name=${name}&number=${number}&is_children=1&trans_device_code=${route.query.number}`
//     // window.open(url)
//     console.log(url)
// }
function downloadAction() {
    window.open(`${config.baseApi}/download/deviceTemplateDownload`)
}

// 新建
const activeData = ref(null)
import newModal from '@/components/fireControl/deviceNew.vue'
const newModalVisible = ref(false)
const newModalTitle = ref('')
function newAction() {
    activeData.value = null
    newModalVisible.value = true
    newModalTitle.value = '新增设备'
    deviceNumber.value = route.query.number
}
function editAction(item) {
    activeData.value = item
    newModalTitle.value = '编辑设备'
    newModalVisible.value = true
}
function onAddSuccess() {
    getData(currentPage.value)
}
// 通知
import noticeModal from '@/components/fireControl/deviceNotice.vue'
const noticeModalVisible = ref(false)
function noticeAction(item) {
    activeData.value = item
    noticeModalVisible.value = true
}
// 阈值
import threshold from '@/components/fireControl/deviceThreshold.vue'
const thresholdModalVisible = ref(false)
function thresholdAction(item) {
    activeData.value = item
    thresholdModalVisible.value = true
}
// 删除
function deleteAction(item) {
    showConfirm({
        content: '您确定要删除该数据吗',
        onConfirm: () => {
            deleteDevice({ id: item.id }).then(() => {
                showMsg('删除成功')
                getData(currentPage.value)
            })
        },
    })
}
const ruleFormRef = ref()
function resetcAction(formEl) {
    // delete filterForm.name
    // delete filterForm.nubmer
    // delete filterForm.current_state
    // console.log(filterForm, 'filterForm')//之前重置搜索
    if (!formEl) return
    formEl.resetFields()
}
</script>
<template>
    <threshold
        v-model:visible="thresholdModalVisible"
        :activeData="activeData"
    />
    <!-- 新建 -->
    <newModal
        v-model:visible="newModalVisible"
        v-if="newModalVisible"
        :title="newModalTitle"
        :deviceId="activeData && activeData.id"
        :deviceClassId="deviceClassId"
        deviceClassify="2"
        :deviceNumber="deviceNumber"
        @success="onAddSuccess"
    ></newModal>
    <!-- 导入 -->
    <importModal
        v-model:visible="importModalVisible"
        v-if="importModalVisible"
        :deviceClassId="deviceClassId"
        deviceClassify="2"
        @success="onImportSuccess"
    ></importModal>
    <noticeModal
        v-model:visible="noticeModalVisible"
        :activeData="activeData"
    ></noticeModal>
    <div class="filter-box white-box d-flex">
        <el-form
            :inline="true"
            :model="filterForm"
            class="d-flex-1"
            ref="ruleFormRef"
        >
            <el-form-item label="设备名称" prop="name">
                <el-input v-model="filterForm.name" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="设备号" prop="number">
                <el-input v-model="filterForm.number" placeholder="请输入" />
            </el-form-item>
            <el-form-item label="设备状态" prop="currentState">
                <el-select
                    v-model="filterForm.currentState"
                    placeholder="请选择"
                >
                    <el-option label="正常" value="1" />
                    <el-option label="报警 " value="2" />
                    <el-option label="离线 " value="3" />
                    <el-option label="故障 " value="4" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchAction">搜索</el-button>
                <el-button
                    type="primary"
                    plain
                    @click="resetcAction(ruleFormRef)"
                >
                    重置
                </el-button>
            </el-form-item>
        </el-form>
        <div class="el-form-item">
            <el-button type="primary" @click="newAction">新增</el-button>
            <el-button type="primary" @click="importModalVisible = true">
                设备导入
            </el-button>
            <!-- <el-button type="primary" @click="exportAction"> 导出 </el-button> -->
            <el-button type="primary" @click="downloadAction">
                模板下载
            </el-button>
        </div>
    </div>
    <div class="white-box mt-20 d-flex-1 overflow-auto">
        <el-table :data="dataList" class="my-10 full" v-loading="loading">
            <el-table-column label="设备编号" prop="number" />
            <el-table-column label="设备名称" prop="name" />
            <el-table-column label="设备类型" prop="device_type_name" />
            <el-table-column label="安装位置" prop="position" />
            <!-- <el-table-column label="安装人员" prop="install_person" /> -->
            <el-table-column label="安装时间" prop="installTime" />
            <!-- <el-table-column label="报警电话" prop="alarm_phone" /> -->
            <el-table-column label="设备状态" prop="current_state">
                <template #default="scope">
                    <span
                        v-if="scope.row.current_state == 1"
                        class="color-success"
                    >
                        •正常
                    </span>
                    <span
                        v-else-if="scope.row.current_state == 2"
                        class="color-warning"
                    >
                        •报警
                    </span>
                    <span
                        v-else-if="scope.row.current_state == 3"
                        class="color-info"
                    >
                        •离线
                    </span>
                    <span
                        v-else-if="scope.row.current_state == 4"
                        class="color-danger"
                    >
                        •故障
                    </span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="150">
                <template #default="scope">
                    <el-tooltip
                        effect="dark"
                        content="通知"
                        placement="top"
                        v-if="false"
                    >
                        <el-button
                            type="primary"
                            link
                            @click="noticeAction(scope.row)"
                        >
                            <el-icon size="16"><Notification /></el-icon>
                        </el-button>
                    </el-tooltip>
                    <el-tooltip effect="dark" content="编辑" placement="top">
                        <el-button
                            type="primary"
                            link
                            @click="editAction(scope.row)"
                        >
                            <el-icon size="16"><Edit /></el-icon>
                        </el-button>
                    </el-tooltip>
                    <el-tooltip
                        effect="dark"
                        content="阈值"
                        placement="top"
                        v-if="false"
                    >
                        <el-button
                            type="primary"
                            link
                            @click="thresholdAction(scope.row)"
                        >
                            <el-icon size="16"><Odometer /></el-icon>
                        </el-button>
                    </el-tooltip>
                    <el-tooltip effect="dark" content="删除" placement="top">
                        <el-button
                            type="primary"
                            link
                            @click="deleteAction(scope.row)"
                        >
                            <el-icon size="16"><Delete /></el-icon>
                        </el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div class="fl-bw">
            <div></div>
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                layout="prev, pager, next, jumper"
                v-model:total="total"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>
<style lang="scss" scoped></style>
